cmake_minimum_required(VERSION 3.10)
project(sqlite3_static C)

# 设置输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)

# 设置版本号
set(SQLITE_VERSION_MAJOR 3)
set(SQLITE_VERSION_MINOR 44)
set(SQLITE_VERSION_PATCH 0)
set(SQLITE_VERSION "${SQLITE_VERSION_MAJOR}.${SQLITE_VERSION_MINOR}.${SQLITE_VERSION_PATCH}")

# SQLite 源文件
set(SQLITE_SOURCE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/sqlite/sqlite3.c")
set(SQLITE_HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/sqlite/sqlite3.h")

# SQLite 编译选项
add_definitions(
    -DSQLITE_ENABLE_RTREE
    -DSQLITE_ENABLE_FTS4
    -DSQLITE_ENABLE_FTS5
    -DSQLITE_ENABLE_JSON1
    -DSQLITE_ENABLE_COLUMN_METADATA
    -DSQLITE_THREADSAFE=1
    -DSQLITE_USE_URI=1
    -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1
    -DSQLITE_ENABLE_UNLOCK_NOTIFY
    -DSQLITE_ENABLE_DBSTAT_VTAB
    -DSQLITE_SECURE_DELETE
)

# 创建静态库
add_library(sqlite3_static STATIC ${SQLITE_SOURCE_FILE})

# 设置库的属性
set_target_properties(sqlite3_static PROPERTIES
    OUTPUT_NAME sqlite3
    C_STANDARD 11
    C_STANDARD_REQUIRED ON
    POSITION_INDEPENDENT_CODE ON
    PUBLIC_HEADER ${SQLITE_HEADER_FILE}
)



# Mac特定的编译选项
target_compile_options(sqlite3_static PRIVATE
    -O2                  # 优化级别
    -fPIC               # 位置无关代码
    -Wall               # 启用警告
    -Wextra
    # -arch x86_64        # 支持 x86_64 架构
    -arch arm64         # 支持 M1/M2 芯片
)

# 安装规则
install(TARGETS sqlite3_static
    ARCHIVE DESTINATION lib
    PUBLIC_HEADER DESTINATION include
)

# 提供编译信息
target_include_directories(sqlite3_static PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/sqlite>
    $<INSTALL_INTERFACE:include>
)
